package database;

import baseclasses.Assignment;
import baseclasses.Reminder;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ReminderDAO {

	public static long addReminder(Context context, Reminder reminder){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.ASSIGNMENTID, reminder.getAssignmentId());
		values.put(DatabaseHelper.REMINDERINTEGER, reminder.getInteger());
		values.put(DatabaseHelper.REMINDERUNITS, reminder.getUnits());

		long result = db.insert(DatabaseHelper.REMINDERTABLE, null, values);

		db.close();
		Log.d("REMINDER","Reminder  with _id ="+reminder.getId()+" added");
		return  result;

	}

	public static Reminder getReminderByAssignment(Context context, Assignment assignment){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		Cursor cursor = db.query(DatabaseHelper.REMINDERTABLE, null,DatabaseHelper.ASSIGNMENTID+" = "+assignment.getId(),null,null,null,null);

		Reminder reminder = null;
		if(cursor.moveToNext()){
			reminder = new Reminder();
			reminder.setId(cursor.getInt(0));
			reminder.setAssignmentId(cursor.getInt(1));
			reminder.setInteger(cursor.getInt(2));
			reminder.setUnits(cursor.getString(3));
		}
		cursor.close();
		db.close();

		return reminder;
	}



	public static int deleteReminder(Context context, Reminder reminder){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		int result =  db.delete(DatabaseHelper.REMINDERTABLE, "_id ="+reminder.getId(), null);
		db.close();
		Log.d("REMINDER","Reminder  with _id ="+reminder.getId()+" deleted");
		return result;
	}

	public static int editReminder(Context context, Reminder reminder){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.ASSIGNMENTID, reminder.getAssignmentId());
		values.put(DatabaseHelper.REMINDERINTEGER, reminder.getInteger());
		values.put(DatabaseHelper.REMINDERUNITS, reminder.getUnits());

		int result = db.update(DatabaseHelper.REMINDERTABLE, values, "_id = "+reminder.getId(), null);
		db.close();

		return result;

	}

	public static int deleteReminderByAssignment(Context context, Assignment assignment){

		DatabaseHelper databaseHelper = new DatabaseHelper(context);
		SQLiteDatabase db = databaseHelper.getWritableDatabase();

		int result = db.delete(DatabaseHelper.REMINDERTABLE, DatabaseHelper.ASSIGNMENTID+"="+assignment.getId(), null);
		Log.d("REMINDER","Reminder  with ASSIGNMENT_id ="+assignment.getId()+" deleted");
		db.close();
		return result;
	}

}
